package com.ruoyi.flowable.listener.scrap;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.equ.domain.EquScrap;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * 报废-3监听：主管副院长审批
 */
@Slf4j
@Component("scrapListener_3")
public class ScrapListener_3 implements ExecutionListener {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void notify(DelegateExecution execution) {
        try {
            //主管副院长审批
            String flag6 = execution.getVariable("flag6", String.class);
            String stopReason = execution.getVariable("stopReason", String.class);
            HashMap<String, Object> masterMap = new HashMap<>();
            masterMap.put("院长审批", flag6);
            masterMap.put("暂缓原因", stopReason);

            EquScrap equScrap = execution.getVariable("equScrap", EquScrap.class);

            HashMap<String, Object> approvalSummaryMap = objectMapper.readValue(equScrap.getApprovalSummary(), new TypeReference<HashMap<String, Object>>() {});

            approvalSummaryMap.put("主管副院长审批", masterMap);
            String approvalSummary = objectMapper.writeValueAsString(approvalSummaryMap);

            equScrap.setApprovalSummary(approvalSummary);

            execution.setVariable("equScrap", equScrap);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
